package com.hlf.modules.api.service.impl;
import java.util.Date;
import java.util.List;

import com.hlf.common.utils.IdWorkerFactory;
import com.hlf.modules.api.service.CheckcodeService;
import com.hlf.common.constants.Constants;
import com.hlf.common.page.Page;
import com.hlf.common.page.PageReturn;
import com.hlf.modules.api.mapper.CheckcodeMapper;
import com.hlf.modules.api.entity.Checkcode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 验证码服务层接口实现类
 *
 */
@Service
public class CheckcodeServiceImpl implements CheckcodeService {

    @Autowired
    CheckcodeMapper checkcodeMapper;

    @Override
    public String deleteById(Integer id) {
        checkcodeMapper.deleteByPrimaryKey(id);
        return Constants.DEFAULT_MSG_SUCCESS;
    }

    @Override
    public String insert(Checkcode record) {
        record.setCodId(IdWorkerFactory.nextId());
        checkcodeMapper.insert(record);
        return Constants.DEFAULT_MSG_SUCCESS;
    }

    @Override
    public String insertInLog(Checkcode record) {
        record.setCodId(IdWorkerFactory.nextId());
        checkcodeMapper.insert(record);
        return Constants.DEFAULT_MSG_SUCCESS;
    }

    @Override
    public String insertSelective(Checkcode record) {
        checkcodeMapper.insertSelective(record);
        return Constants.DEFAULT_MSG_SUCCESS;
    }

    @Override
    public Checkcode selectById(Integer id) {
        return checkcodeMapper.selectByPrimaryKey(id);
    }

    @Override
    public String updateByIdSelective(Checkcode record) {
        checkcodeMapper.updateByPrimaryKeySelective(record);
        return Constants.DEFAULT_MSG_SUCCESS;
    }

    @Override
    public String updateById(Checkcode record) {
        checkcodeMapper.updateByPrimaryKey(record);
        return Constants.DEFAULT_MSG_SUCCESS;
    }

    @Override
    public List<Checkcode> selectAll() {
        return checkcodeMapper.select(null);
    }

    @Override
    public PageReturn<Checkcode> selectForPage(Checkcode entity, Page page) {
        int count = checkcodeMapper.selectForPageCount(entity, page);
        List<Checkcode> list = checkcodeMapper.selectForPage(entity, page);
        return new PageReturn<Checkcode>(list, count, page, entity);
    }

    @Override
    public Checkcode findNearestCode(String mobile, String code, int type) {
        return checkcodeMapper.findNearestCode(mobile, code, type);
    }

    @Override
    public void changeCodeUsed(Long id) {
        Checkcode checkcode = new Checkcode();
        checkcode.setCodId(id);
        checkcode.setCodUsed(Constants.COD_USED_YES);
        checkcode.setCodCheckTime(new Date());
        updateByIdSelective(checkcode);
    }

    @Override
    public int getDifftimeByNow(String mobile) {
        Integer result = checkcodeMapper.getDifftimeByNow(mobile);
        if(result == null){
            return 60;
        }
        return result;
    }

    @Override
    public boolean getCodIdByNowAndLimitCount(String mobile, Integer limitCount,Integer hoursNum) {
        Long result = checkcodeMapper.getCodIdByNowAndLimitCount(mobile, limitCount,hoursNum);
        if(result==null){
            return false;
        }
        return true;
    }

}
